//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.ui;

import tv.dyndns.kishibe.client.Utility;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class WidgetMultiItemSelector extends VerticalPanel implements ClickHandler {
	private final ListBox listBox = new ListBox();
	private final CheckBox checkBoxMultiSelect = new CheckBox("複数選択");
	private final CheckBox[] checkBoxs;
	private final HorizontalPanel panelMultiSelect = new HorizontalPanel();

	public WidgetMultiItemSelector(String title, String[] items, int columns) {
		setHorizontalAlignment(ALIGN_CENTER);

		// 上段
		{
			listBox.setWidth("150px");
			for (String item : items) {
				listBox.addItem(item);
			}

			checkBoxMultiSelect.addClickHandler(this);

			final HorizontalPanel panel = new HorizontalPanel();
			panel.setVerticalAlignment(ALIGN_MIDDLE);
			panel.add(new Label(title));
			panel.add(listBox);
			panel.add(checkBoxMultiSelect);
			add(panel);
		}

		// 下段
		{
			final VerticalPanel[] panels = new VerticalPanel[columns];
			for (int i = 0; i < panels.length; ++i) {
				panels[i] = new VerticalPanel();
				panelMultiSelect.add(panels[i]);
			}

			checkBoxs = new CheckBox[items.length];
			for (int i = 0; i < items.length; ++i) {
				checkBoxs[i] = new CheckBox(items[i]);
				panels[i / ((items.length + columns - 1) / columns)].add(checkBoxs[i]);
			}
			add(panelMultiSelect);
		}

		set(0);
	}

	public void set(int flag) {
		if (flag == 0) {
			flag = 1;
		}

		if (Utility.getInstance().countBits(flag) == 1) {
			checkBoxMultiSelect.setValue(false);
			listBox.setSelectedIndex(Utility.getInstance().numberOfTrainingZero(flag) - 1);
		} else {
			checkBoxMultiSelect.setValue(true);
			for (int i = 0; i < checkBoxs.length; ++i) {
				checkBoxs[i].setValue((flag & (1 << i)) != 0);
			}
		}

		updateWidget();
	}

	public int get() {
		final boolean multiSelect = isMultiSelect();

		int result = 0;
		if (!multiSelect) {
			result = 1 << listBox.getSelectedIndex();
		} else {
			for (int i = 0; i < checkBoxs.length; ++i) {
				if (checkBoxs[i].getValue()) {
					result |= (1 << i);
				}
			}
		}

		return result;
	}

	private void updateWidget() {
		final boolean multiSelect = isMultiSelect();

		listBox.setVisible(!multiSelect);
		panelMultiSelect.setVisible(multiSelect);
	}

	public boolean isMultiSelect() {
		return checkBoxMultiSelect.getValue();
	}

	public void setEnabled(boolean enabled) {
		listBox.setEnabled(enabled);
		checkBoxMultiSelect.setEnabled(enabled);
		for (CheckBox checkBox : checkBoxs) {
			checkBox.setEnabled(enabled);
		}
	}

	@Override
	public void onClick(ClickEvent event) {
		final Widget sender = (Widget) event.getSource();
		if (sender == checkBoxMultiSelect) {
			updateWidget();
		}
	}
}
